package com.bourne.controller;

import com.bourne.annotation.CountTime;
import com.bourne.constant.StringConstant;
import com.bourne.enums.BizCodeEnum;
import com.bourne.interceptor.AuthInterceptor;
import com.bourne.model.LoginUser;
import com.bourne.model.UserDO;
import com.bourne.request.UserLoginRequest;
import com.bourne.request.UserRegisterRequest;
import com.bourne.service.impl.UserServiceImpl;
import com.bourne.util.CommonUtil;
import com.bourne.util.InterceptorUtil;
import com.bourne.util.JsonData;
import com.bourne.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
//import org.flowable.bpmn.model.BpmnModel;
//import org.flowable.engine.*;
//import org.flowable.engine.runtime.Execution;
//import org.flowable.engine.runtime.ProcessInstance;
//import org.flowable.image.ProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserController
 * @Description 用户模块
 * @Author bourne
 * @Date 2021/9/7 12:28
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/user/v1")
@Slf4j
@CrossOrigin
@Api(tags = "用户模块")
@RefreshScope
public class UserController extends BaseCoreController<UserServiceImpl, UserDO> {

    /**
     * 用户注册
     * @param registerRequest
     * @return
     */
    @PostMapping("register")
    public JsonData register(@Validated @RequestBody @ApiParam("用户注册对象") UserRegisterRequest registerRequest){
        return service.register(registerRequest);
    }


    /**
     * 用户登录
     * @param loginRequest
     * @param request
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody @ApiParam("用户登录对象") UserLoginRequest loginRequest, HttpServletRequest request){
        return service.login(loginRequest, request);
    }

    /**
     * 个人信息查询
     * @return
     */
    @CountTime
    @PostMapping("detail")
    public JsonData detail(HttpServletRequest request){
        LoginUser loginUser = AuthInterceptor.threadLocal.get();
        if (loginUser == null) {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_INVALID);
        }
        UserVO userVo = service.findUserDetail(loginUser.getId());
        return JsonData.buildSuccess(userVo);
    }


    /**
     * 测试
     * @return
     */
    @GetMapping("testSql")
    public JsonData testSql(UserDO userDO){
        Object o = service.testSql(userDO);
        return JsonData.buildSuccess(o);
        // return JsonData.buildSuccess(true);
    }

//    @Autowired
//    RuntimeService runtimeService;
//
//    @Autowired
//    TaskService taskService;
//
//    @Autowired
//    RepositoryService repositoryService;
//
//    @Autowired
//    ProcessEngine processEngine;
//
//    @GetMapping("/pic")
//    public void showPic(HttpServletResponse resp, String processId) throws Exception {
//        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
//        if (pi == null) {
//            return;
//        }
//        List<Execution> executions = runtimeService
//                .createExecutionQuery()
//                .processInstanceId(processId)
//                .list();
//
//        List<String> activityIds = new ArrayList<>();
//        List<String> flows = new ArrayList<>();
//        for (Execution exe : executions) {
//            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
//            activityIds.addAll(ids);
//        }
//
//        /**
//         * 生成流程图
//         */
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
//        ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
//        ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
//        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, false);
//        OutputStream out = null;
//        byte[] buf = new byte[1024];
//        int legth = 0;
//        try {
//            out = resp.getOutputStream();
//            while ((legth = in.read(buf)) != -1) {
//                out.write(buf, 0, legth);
//            }
//        } finally {
//            if (in != null) {
//                in.close();
//            }
//            if (out != null) {
//                out.close();
//            }
//        }
//    }
}
